package com.java并发编程.exchange;

import java.util.concurrent.Exchanger;

public class ExchangeTest {
	
	public static void main(String[] args) {
		final Exchanger<String> exchanger = new Exchanger<>();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String exchange = exchanger.exchange("好A");
					System.out.println(Thread.currentThread().getName() + " 得到 " + exchange);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "A").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String exchange = exchanger.exchange("好B");
					System.out.println(Thread.currentThread().getName() + " 得到 " + exchange);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "B").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String exchange = exchanger.exchange("好C");
					System.out.println(Thread.currentThread().getName() + " 得到 " + exchange);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "C").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String exchange = exchanger.exchange("好D");
					System.out.println(Thread.currentThread().getName() + " 得到 " + exchange);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "D").start();
	}

}
